package com.dhsc.service.impl.wdywgl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dhsc.domain.ywzx.wdywgl.CsbmdVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.wdywgl.ICsbmdDao;
import com.dhsc.service.wdywgl.ICsbmdService;
import com.dhsc.util.CommonUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: CsbmdServiceImpl
 * @Description: 测试白名单
 * @date: 2023/12/1 13:12
 * @author: gx
 */
@Service
@Slf4j
public class CsbmdServiceImpl implements ICsbmdService {

    @Autowired
    private ICsbmdDao csbmdDao;

    @Override
    public LayuiTableVo queryCsbmd(CsbmdVo csbmdVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            //状态不为空的话，说明是导出excel发出的请求，不需要开启pageHelper
            if (!CommonUtil.isNotEmptyObj(csbmdVo.getState())){
                PageMethod.startPage(csbmdVo.getPage(),csbmdVo.getLimit());
            }
            List<CsbmdVo> list = csbmdDao.queryCsbmd(csbmdVo);
            PageInfo<CsbmdVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
            obj.setMsg("");
        }catch (Exception e){
            log.error("查询测试白名单异常",e);
            throw new MyException("查询测试白名单异常",e);
        }
        return obj;
    }

    @Override
    public ResponseObject saveCsbmd(CsbmdVo csbmdVo) {
        ResponseObject obj = new ResponseObject();
        try {
            CsbmdVo c = csbmdDao.queryQxByJgdm(csbmdVo);
            if (!CommonUtil.isNotEmptyObj(c)){
                obj.setStatus(202);
                obj.setMessage("该客户号无法添加，请检查客户状态及权限");
                return obj;
            }
            if (!CommonUtil.isNotEmptyObj(csbmdVo.getBh())){
                if(csbmdVo.getBmxz().equals("1")){
                    //查询客户号是否真的存在
                    csbmdVo.setYyb(c.getYyb());
                }
                csbmdDao.insertCsbmd(csbmdVo);
            }else{
                csbmdDao.updateCsbmd(csbmdVo);
            }
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("保存测试白名单出错", e);
            throw new MyException("保存测试白名单出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject deleteCsbmd(CsbmdVo csbmdVo) {
        ResponseObject obj = new ResponseObject();
        try {
            csbmdDao.deleteCsbmd(csbmdVo);
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("删除测试白名单出错", e);
            throw new MyException("删除测试白名单出错",e);
        }
        return obj;
    }
}
